package com.semac.client.componentes.objectscombo;

import java.util.HashMap;
import java.util.LinkedList;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.ListBox;

public abstract  class ObjectsCombo<T> extends ListBox implements ChangeHandler {
	
	
	private HashMap<String,T> objectMapping = new HashMap<String, T>();
	private LinkedList<T> valuesList;
	private String defaultValue;

	
	public ObjectsCombo(String defaultValue) {
		this.addItem(defaultValue);
		this.defaultValue = defaultValue;
		this.addChangeHandler(this);
	}	
	
	
	@Override
	public void clear() {
		super.clear();
		this.addItem(defaultValue);
		this.objectMapping.clear();
	}
	
	public void addItem(T item) {
		this.valuesList.add(item);
	}
	
	public void addItems(LinkedList<T> items){
		for (T t : items) {
			this.addItem(t);
		}
	}
	
	private void render(){
		for (T value : this.valuesList) {
			this.addItem(this.getValue(value));
			this.objectMapping.put(this.getValue(value), value);
		}
	}
	
	protected abstract String getValue(T object);


	@Override
	public void onChange(ChangeEvent event) {
		String selectedItem = this.getItemText(this.getSelectedIndex());
		if (selectedItem.equals(this.defaultValue))
//			this.fireEvent(new )
			;
	}
	
}
